package com.qymerge.manage.base.dao.impl;

import com.qymerge.manage.base.dao.IMetaOrgDao;
import com.qymerge.manage.base.dao.IMetaOrgFeeCompanyCostCenterDao;
import com.qymerge.manage.base.ro.MetaOrgFeeCompanyCostCenterRO;
import com.qymerge.manage.base.ro.MetaOrgIncomeCompanyRO;
import com.qymerge.manage.core.bean.PageRequest;
import com.qymerge.manage.core.dao.BaseDao;
import org.apache.commons.lang.StringUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *<p>Title: MetaOrgFeeCompanyCostCenterDaoImpl</p>
 *<p>Description: 费用的来源公司和成本中心</p>
 *
 * @author Xiao
 * @date 2025/5/2 01:23
 * @version 1.0.0
 */
@Repository
public class MetaOrgFeeCompanyCostCenterDaoImpl extends BaseDao implements IMetaOrgFeeCompanyCostCenterDao {


//    private String dwd_MCRS_MDM_meta_OrgFeeCompanyCostCenter = "SELECT Id,OrgId,CompanyId,CostCenterId FROM dwd_MCRS_MDM_meta_OrgFeeCompanyCostCenter WHERE 1=1 ";
    private String dwd_MCRS_MDM_meta_OrgFeeCompanyCostCenter = "SELECT occ.Id as Id, occ.OrgId as OrgId, occ.CompanyId as CompanyId, occ.CostCenterId as CostCenterId ,o.Id as OrgID,o.Name as OrgName,c.Id as CompanyID,c.Code as CompanyCode,c.Name as CompanyName,cc.Id as CostCenterID,cc.Code as CostCenterCode, cc.Name as CostCenterName" +
        "    FROM dwd_MCRS_MDM_meta_OrgFeeCompanyCostCenter occ" +
        "    left join dwd_MCRS_MDM_meta_Org o on occ.OrgId = o.Id" +
        "    left join dwd_MCRS_MDM_meta_Company c on occ.CompanyId = c.Id" +
        "    left join dwd_MCRS_MDM_meta_CostCenter cc on occ.CostCenterId = cc.Id WHERE 1=1 ";

    @Override
    public List list(MetaOrgFeeCompanyCostCenterRO ro, PageRequest pageRequest) {
        int skip = (pageRequest.getPage() - 1) * pageRequest.getSize();
        int size = pageRequest.getSize();

        String sql = dwd_MCRS_MDM_meta_OrgFeeCompanyCostCenter;
        Map<String, Object> paramMap = new HashMap<String, Object>();

        sql += " and occ.OrgId=:OrgId";
        paramMap.put("OrgId", ro.getOrgId());

        // 分页数据获取
        sql += " order by occ.Id desc limit :skip,:sizes";
        paramMap.put("skip", skip);
        paramMap.put("sizes", size);

        return getNamedParameterJdbcTemplate().query(sql, paramMap, new BeanPropertyRowMapper<MetaOrgFeeCompanyCostCenterRO>(MetaOrgFeeCompanyCostCenterRO.class));
    }

    @Override
    public int total(MetaOrgFeeCompanyCostCenterRO ro) {
        String sql = "select count(1) from dwd_MCRS_MDM_meta_OrgFeeCompanyCostCenter where 1=1";
        Map<String, Object> paramMap = new HashMap<String, Object>();

        sql += " and OrgId=:OrgId";
        paramMap.put("OrgId", ro.getOrgId());

        return getNamedParameterJdbcTemplate().queryForObject(sql, paramMap, Integer.class);
    }

    @Override
    public MetaOrgFeeCompanyCostCenterRO findOne(Long id) {
        StringBuffer sql = new StringBuffer(dwd_MCRS_MDM_meta_OrgFeeCompanyCostCenter);
        sql.append("and occ.Id=?");
        RowMapper<MetaOrgFeeCompanyCostCenterRO> rowMapper = new BeanPropertyRowMapper<MetaOrgFeeCompanyCostCenterRO>(MetaOrgFeeCompanyCostCenterRO.class);
        try {
            return getJdbcTemplate().queryForObject(sql.toString(), rowMapper, id);
        } catch (DataAccessException e) {
            return null;
        }
    }

    @Override
    public int insert(MetaOrgFeeCompanyCostCenterRO ro) {
        StringBuilder sql = new StringBuilder("insert into dwd_MCRS_MDM_meta_OrgFeeCompanyCostCenter(OrgId,CompanyId, CostCenterId) values(?,?,?)");

        List params = new ArrayList();
        params.add(ro.getOrgId());
        params.add(ro.getCompanyId());
        params.add(ro.getCostCenterId());

        return getJdbcTemplate().update(sql.toString(), params.toArray());
    }

    @Override
    public int remove(List<Long> ids) {
        // 删除
        String sql="delete from dwd_MCRS_MDM_meta_OrgFeeCompanyCostCenter where Id=?";
        final List<Long> idss = ids;
        BatchPreparedStatementSetter setterMid = new BatchPreparedStatementSetter(){

            @Override
            public int getBatchSize(){
                return idss.size();
            }

            @Override
            public void setValues(PreparedStatement ps, int i){
                Long id = idss.get(i);
                try {
                    ps.setLong(1, id);
                } catch(Exception e){
                    logger.error("batchInsertWaitAward Exception:", e);
                }
            }
        };

        return getJdbcTemplate().batchUpdate(sql, setterMid).length;
    }


}
